<?php

namespace App\Service;

use App\Model\SaErrorLog;
use App\Model\SaAdminLoginLog;

class LogService
{
    /**
     * 获取登录日志.
     *
     * @param $param
     * @param $limit
     *
     * @return mixed
     */
    public static function getLoginLog($param, $limit)
    {
        $where = [];
        if (array_key_exists('username', $param) && !empty($param['username'])) {
            $where[] = ['a.username', '=', $param['username']];
        }
        if (array_key_exists('start', $param) && !empty($param['start'])) {
            $where[]   = ['all.created_at', '>=', $param['start']];
        }
        if (array_key_exists('end', $param) && !empty($param['end'])) {
            $where[] = ['all.created_at', '<=', $param['end']];
        }

        $result = SaAdminLoginLog::getLoginLog($where, $limit);

        return $result;
    }

    /**
     * 获取错误日志.
     *
     * @param $param
     * @param $limit
     *
     * @return mixed
     */
    public static function getErrorLog($param, $limit)
    {
        $where = [];
        if (array_key_exists('status', $param) && null !== $param['status']) {
            $where[] = ['status', '=', $param['status']];
        }
        if (array_key_exists('start', $param) && !empty($param['start'])) {
            $where[]   = ['created_at', '>=', $param['start']];
        }
        if (array_key_exists('end', $param) && !empty($param['end'])) {
            $where[] = ['created_at', '<=', $param['end']];
        }

        $result = SaErrorLog::getErrolLog($where, $limit);

        return $result;
    }

    /**
     * 统计错误日志数量.
     *
     * @param $param
     *
     * @return mixed
     */
    public static function getErrorCount($param)
    {
        $where = [];
        if (array_key_exists('status', $param) && null !== $param['status']) {
            $where[] = ['status', '=', $param['status']];
        }

        $result = SaErrorLog::getErrorCount($where);

        return $result;
    }
}
